package com.taobao.tql.parser;

import java.io.StringReader;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;


public class JsqlparserTest {
	
	public void testReplace() {
		String tableStr = "taobao.user..com.get";
		tableStr = tableStr.substring(0, tableStr.lastIndexOf("."));
		//System.out.print(tableStr);
		tableStr = tableStr.substring(tableStr.indexOf(".") + 1, tableStr.length());
		//System.out.print(tableStr);
		System.out.print(tableStr + "\n");
		tableStr = tableStr.replaceAll("\\.", "");
		System.out.print(tableStr);
	}
	
	public void test2() {
		CCJSqlParserManager pm = new CCJSqlParserManager();
		//String sql = "SELECT * FROM MY_TABLE1, MY_TABLE2, (SELECT * FROM MY_TABLE3) LEFT OUTER JOIN MY_TABLE4 "+
		//" WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)" ;
		
		String sql = "SELECT aa,aa2,aa3,aa4 FROM MY_TABLE1.get.com, MY_TABLE2 where a = a and b = b " ;
		net.sf.jsqlparser.statement.Statement statement = null;
		try {
			statement = pm.parse(new StringReader(sql));
		} catch (JSQLParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/* 
		now you should use a class that implements StatementVisitor to decide what to do
		based on the kind of the statement, that is SELECT or INSERT etc. but here we are only
		interested in SELECTS
		*/
		if (statement instanceof Select) {
			Select selectStatement = (Select) statement;
			PlainSelect plainSel = (PlainSelect) selectStatement.getSelectBody();
			System.out.println(plainSel.getWhere());
			System.out.println(plainSel.getFromItem());
			System.out.println(plainSel.getSelectItems());
			
			/*TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
			List tableList = tablesNamesFinder.getTableList(selectStatement);
			for (Iterator iter = tableList.iterator(); iter.hasNext();) {
				System.out.println(iter.next());
			}
			List tableList2 = tablesNamesFinder.getColumnsList(selectStatement);
			for (Iterator iter = tableList2.iterator(); iter.hasNext();) {
				System.out.println(iter.next());
			}
			//
*/			
		}
	}
	public static void main( String[] args)
    {
		JsqlparserTest test = new JsqlparserTest();
		test.testReplace();
		
    }
}
